package com.yuan.zs.ui.me;

import android.os.Bundle;
import android.widget.TextView;

import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.bean.TipResult;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.base.utils.DialogUtil;
import com.yuan.zs.R;
import com.yuan.zs.Urls;
import com.yuan.zs.bean.Child;
import com.yuan.zs.common.CacheManager;

import java.util.HashMap;

/**
 * 幼儿信息
 */
public class ChildInfoActivity extends BaseFrameworkActivity {

	// --------------------------控件--------------------------
	private TextView tv_child_name;
	private TextView tv_child_id_card;
	private TextView tv_parent_sex;
	private TextView tv_child_school;
	private TextView tv_child_grade;
	// --------------------------变量--------------------------
	private Child child;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_child_info;
	}

	@Override
	protected void bindViews() {
		tv_child_name = findViewById(R.id.tv_child_name);
		tv_child_id_card = findViewById(R.id.tv_child_id_card);
		tv_parent_sex = findViewById(R.id.tv_parent_sex);
		tv_child_school = findViewById(R.id.tv_child_school);
		tv_child_grade = findViewById(R.id.tv_child_grade);
		setOnClickListener(R.id.btn_untie_submit, v -> {
			DialogUtil.confirm(this, "提示", "确认解绑吗？", "确定", (dialog, which) -> {
				doUntie();
			}, "取消", null);
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		child = getIntent().getParcelableExtra("child");
		if (child == null) {
			closeSelf();
			return;
		}
		init();
	}

	private void init() {
		tv_child_name.setText(child.getName());
		tv_child_id_card.setText(child.getIdCardNo());
		tv_parent_sex.setText(child.getGender());
		tv_child_school.setText(child.getSchoolName());
		tv_child_grade.setText(child.getGradeName());
//		CacheManager.getInstance().getSchool(child.getSchoolId(), new CacheManager.HandleCallback<School>() {
//			@Override
//			public void onCacheSuccess(School result) {
//				tv_child_school.setText(result.getName());
//			}
//
//			@Override
//			public void onHttpSuccess(School result) {
//				tv_child_school.setText(result.getName());
//			}
//
//			@Override
//			public void onError() {
//				tv_child_school.setText("");
//			}
//		});
//		CacheManager.getInstance().getGrade(child.getSchoolId(), child.getGradeId(), new CacheManager.HandleCallback<Grade>() {
//			@Override
//			public void onCacheSuccess(Grade result) {
//				tv_child_grade.setText(result.getName());
//			}
//
//			@Override
//			public void onHttpSuccess(Grade result) {
//				tv_child_grade.setText(result.getName());
//			}
//
//			@Override
//			public void onError() {
//				tv_child_grade.setText("");
//			}
//		});
	}

	private void doUntie() {
		HashMap<String, Object> params = new HashMap<>();
		params.put("childId", child.getId());
		BaseHttpRequest.post(Urls.getUrl(Urls.uniteChild), params, new BaseHttpCallBack<TipResult>() {
			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					showToast("解绑成功");
					CacheManager.getInstance().deleteChild(child.getId());
					closeSelf();
				} else {
					showToast(result.getMessage());
				}
			}
		});
	}

}

